/**
 * 地图基类
 * @author    Toby
 */

define("dojo/_base/declare dojo/_base/array dojo/_base/lang dojo/dom dojo/on dojo/topic esri/geometry/Extent esri/geometry/Multipoint esri/layers/ArcGISTiledMapServiceLayer esri/layers/GraphicsLayer esri/map esri/config esri/SpatialReference ../BaseWidget".split(" "),
    function (declare, array, lang, dom, on, topic, Extent, Multipoint, ArcGISTiledMapServiceLayer,GraphicsLayer, Map,esriConfig, SpatialReference, BaseWidget) {

        return declare([BaseWidget], {
            templateString:'<div style="display:none;"></div>',

            constructor:function (args) {
                this.settings = lang.mixin({
                    mapId:"map",
                    slider:false,
                    logo:false
                }, args);
               
                this.map = null;
//                esriConfig.defaults.io.proxyUrl = "proxy.jsp";
//    			esriConfig.defaults.io.alwaysUseProxy=true; 
//    			esriConfig.defaults.io.corsDetection = false;
            },

            postMixInProperties:function () {
                this.inherited(arguments);
            },

            postCreate:function () {
                this.inherited(arguments);
            },

            startup:function () {
                this.initMap();
                this.inherited(arguments);
            },

            destroy:function () {
                if (this.map) {
                    this.map.destroy();
                }
                this.inherited(arguments);
            },

            /**
             * 初始化地图
             */
            initMap:function () {
                var s = this.settings,
                    mapId = s.mapId,
                    initialExtent, options, map, basemap;
                initialExtent = new Extent(ioc.map.initExt);

                options = {
                    extent:initialExtent,
                    wrapAround180:true,
                    slider:s.slider,
                    sliderPosition:"bottom-right",
                    logo:s.logo,
                    autoResize:true,
                    fadeOnZoom:true,
                    force3DTransforms:true,
                    navigationMode:"css-transforms",
                    minScale:1500000//73957190.948944//level3//1500000
                };
                map = new Map(mapId, options);
                on(map, "load", lang.hitch(this, this.onMapLoaded, s.onload));
                basemap = new ArcGISTiledMapServiceLayer(ioc.map.basemap);
                basemap.isBasemapLayer = true;	// 标识为基础底图
                map.addLayer(basemap);
                
                for (var i=0;i<ioc.map.graphicLayers.length;i++){
                	var name=ioc.map.graphicLayers[i];
                	var gLayer=new GraphicsLayer({"id":name});
                	map.addLayer(gLayer);
                }
            },

            onMapLoaded:function (onload, args) {
                this.map = args.map;
                if (typeof onload === "function") {
                    onload(this.map);
                }
            },

            /**
             * 获取图形JSON对象数组
             * @param graphics
             */
            getGraphicsJson:function (graphics) {
                if (!graphics) {
                    return null;
                }
                var result = {};

                result.graphics = array.map(graphics, function (g) {
                    return g.toJson();
                });
                return result;
            },

            /**
             * 获取地图范围
             * @return {*}
             */
            getExtent:function () {
                return this.map.extent;
            },

            /**
             * 设置地图范围
             * @return {*}
             */
            setExtent:function (extent) {
                this.map.setExtent(extent);
            }
        });
    });